// 1 引入 vuex  需要先安装
import { createStore } from 'vuex'


import createPersistedState from "vuex-persistedstate"


// 创建一个新的 store 实例
const store = createStore({
    state: {
        count: 10,
        token: '',
        msg: 'vue3 项目',
        username: ''

    },
    mutations: {
        addCount(state) {
            state.count++
        },
        // 存储token 
        setToken(state, payload) {
            state.token = payload
        },

        setUserName(state, payload) {
            state.username = payload
        },
        // 清除token
        clearToken(state, payload) {
            state.token = payload
        }
    },
    actions: {
        asyncAddCount(context) {

            setTimeout(() => {
                context.commit('addCount')
            }, 2000)

        }
    },
    getters: {},
    modules: {},

    //  存储 state所有的数据
    plugins: [createPersistedState({
        storage: window.localStorage

    })],

    // 存储单个数据 自定义
    // plugins: [createPersistedState({
    //   storage: window.localStorage,
    //   reducer (val) {
    //     console.log(val)
    //     return {

    //       // 只储存state中的assessmentData
    //       myMsg: val.msg
    //     }
    //   }
    // })]
})

//3 导出实例
export default store